package com.wangcw.minicat;

import cn.hutool.core.io.FileUtil;
import com.minicat.sdk.MiniCatServlet;
import com.minicat.sdk.Request;
import com.minicat.sdk.Response;
import com.wangcw.minicat.bean.ServerXmlConfigBean;
import com.wangcw.minicat.bean.ThreadConfigBean;
import com.wangcw.minicat.util.XMLReadUtil;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.util.concurrent.ThreadPoolExecutor;

import static cn.hutool.core.util.ClassUtil.getClassLoader;


/**
 * 启动类
 */
public class Application {

    public static void main2(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException,
            IllegalAccessException, InvocationTargetException, InstantiationException {
//        RequestListenter.miniCatServletMap.get()

        BufferedInputStream inputStream = FileUtil.getInputStream("");

    }
    public static void main(String[] args) throws IOException {
        XMLReadUtil xmlReadUtil = new XMLReadUtil();
        ServerXmlConfigBean configServerBean = xmlReadUtil.getConfigServerBean();
        ServerSocket serverSocket = new ServerSocket(configServerBean.getPort());
        ThreadConfigBean threadConfigBean = new ThreadConfigBean();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                threadConfigBean.getCorePoolSize(),
                threadConfigBean.getMaximumPoolSize(),
                threadConfigBean.getKeepAliveTime(),
                threadConfigBean.getUnit(),
                threadConfigBean.getWorkQueue(),
                threadConfigBean.getThreadFactory(),
                threadConfigBean.getHandler()
        );

        while(true) {
            Socket socket = serverSocket.accept();
            RequestListenter requestProcessor = new RequestListenter(socket);
            threadPoolExecutor.execute(requestProcessor);
        }

    }


}
